#!/usr/bin/env python
# 
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util


class GetHeadersHandler(webapp.RequestHandler):
    def get(self):
        head = ""
        for tk, c in self.request.headers.iteritems():
            head += (tk + ":" + c + '\n')
        self.response.headers["Content-Type"] = "text/html; charset=utf-8"
        self.response.out.write( \
"""
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
	<title>View HTTP Request and Response Header</title>
	<meta name="description" content="view request and response header of a HTTP connection, HTTP status codes and HTML source">
	<meta name="keywords" content="HTTP, header, web, charset, status, unicode, BOM, byte order mark, webserver, apache, browser, HTML, IDN, Punycode">
	<link rel="stylesheet" type="text/css" href="/style/basic.css">
</head>

<body>
    <h1>View HTTP Request and Response Header</h1>
    <h2>HTTP Request Header(to here)</h2><pre> %s </pre>
    <form action="/" method="post">
    <fieldset>
      <p>
        <strong>HTTP(S)-URL:</strong>
        <input class="text url" type="text" name="url" value="">
        <input type="submit" name="submit" value="Submit">
        <span style="font-size:0.8em;">(<abbr title="Internationalized Domain Names">IDN</abbr> allowed)</span>
      </p>
      <p>
        Request type:<label for="get-request"><input checked="checked" type="radio" name="type" id="get-request" value="GET"> GET</label>
        <label for="post-request"><input type="radio" name="type" id="post-request" value="POST"> POST</label>
        <label for="head-request"><input type="radio" name="type" id="head-request" value="HEAD"> HEAD</label>
        <label for="trace-request"><input type="radio" name="type" id="trace-request" value="TRACE"> TRACE</label>
      </p>
      
		
    </fieldset>
  </form>


<table cellspacing="0">
<thead>
<tr>
</tr>
</thead> 
</body></html>"""% head)
        


    def Post(self):
        head = ""
        for tk, c in self.request.headers.iteritems():
            head += (tk + ":" + c + '\n')
        url = self.request.get("text url").encode("utf-8")
        
        self.response.headers["Content-Type"] = "text/html; charset=utf-8"
        self.response.out.write( \
"""
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
	<title>View HTTP Request and Response Header</title>
	<meta name="description" content="view request and response header of a HTTP connection, HTTP status codes and HTML source">
	<meta name="keywords" content="HTTP, header, web, charset, status, unicode, BOM, byte order mark, webserver, apache, browser, HTML, IDN, Punycode">
	<link rel="stylesheet" type="text/css" href="/style/basic.css">
</head>

<body>
    <h1>View HTTP Request and Response Header</h1>
    <h2>HTTP Request Header(to here)</h2><pre> %s </pre>
    <form action="/" method="post">
    <fieldset>
      <p>
        <strong>HTTP(S)-URL:</strong>
        <input class="text url" type="text" name="url" value="">
        <input type="submit" name="submit" value="Submit">
        <span style="font-size:0.8em;">(<abbr title="Internationalized Domain Names">IDN</abbr> allowed)</span>
      </p>
      <p>
        Request type:<label for="get-request"><input checked="checked" type="radio" name="type" id="get-request" value="GET"> GET</label>
        <label for="post-request"><input type="radio" name="type" id="post-request" value="POST"> POST</label>
        <label for="head-request"><input type="radio" name="type" id="head-request" value="HEAD"> HEAD</label>
        <label for="trace-request"><input type="radio" name="type" id="trace-request" value="TRACE"> TRACE</label>
      </p>
      <p>
      </p>
		
    </fieldset>
  </form>

<h2>HTTP Request Header</h2><pre>Connect to 69.63.180.48 on port 80 ... ok

GET /x/1158041750/false/p_1153131500=0 HTTP/1.1<span class="crlf" title="x0D0A">[CRLF]</span>
Host: 0.109.channel.facebook.com<span class="crlf" title="x0D0A">[CRLF]</span>
Connection: close<span class="crlf" title="x0D0A">[CRLF]</span>
User-Agent: Web-sniffer/1.0.36 (+http://web-sniffer.net/)<span class="crlf" title="x0D0A">[CRLF]</span>
Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7<span class="crlf" title="x0D0A">[CRLF]</span>
Cache-Control: no<span class="crlf" title="x0D0A">[CRLF]</span>
Accept-Language: de,en;q=0.7,en-us;q=0.3<span class="crlf" title="x0D0A">[CRLF]</span>
Referer: http://web-sniffer.net/<span class="crlf" title="x0D0A">[CRLF]</span>
<span class="crlf" title="x0D0A">[CRLF]</span>
</pre><h2>HTTP Response Header</h2>

<table cellspacing="0">
<thead>
<tr>
	<th class="name">Name</th><th>Value</th><th class="delim">Delim</th>
</tr>
</thead>
<tbody>
<tr><td colspan="3" style="padding:10px 5px;"><strong>Status: HTTP/1.1 200 OK
</strong></td></tr><tr class="swap"><td class="name">Date:</td><td>Fri, 17 Sep 2010 08:14:34 GMT</td><td></td></tr><tr><td class="name">Content-Type:</td><td>text/plain</td><td></td></tr><tr class="swap"><td class="name">Connection:</td><td>close</td><td></td></tr><tr><td class="name">Content-Length:</td><td>37</td><td></td></tr></tbody></table><h2>Content (0.04 <acronym title="KibiByte = 1024 Byte">KiB</acronym>)</h2><div id="html"><pre>for (;;);{"t":"refresh","reason":101}</pre></div>
</body></html>""" % head)
        


application = webapp.WSGIApplication([('/headers.py', GetHeadersHandler)],
                                         debug=True)
